package au.com.handsunited.service.impl;

import java.util.List;
import java.util.Map;
import java.util.TreeMap;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import au.com.handsunited.dao.LanguageDAO;
import au.com.handsunited.model.language.Language;
import au.com.handsunited.service.LanguageService;

/**
 * @author arunc
 *
 */
public class LanguageServiceImpl implements LanguageService {


	private final LanguageDAO languageDAO;

	private static Logger logger = LoggerFactory.getLogger(LanguageServiceImpl.class);

	/**
	 * @param languageDAO
	 */
	public LanguageServiceImpl(LanguageDAO languageDAO) {
		super();
		this.languageDAO = languageDAO;
	}

	/**
	 * @see au.com.handsunited.service.LanguageService#createLanguage(au.com.handsunited.model.language.Language)
	 */
	@Override
	public Language createLanguage(Language language) {

		return languageDAO.create(language);
	}

	/**
	 * @see au.com.handsunited.service.LanguageService#deleteLanguage(au.com.handsunited.model.language.Language)
	 */
	@Override
	public void deleteLanguage(Language language) {
		languageDAO.delete(language);
	}

	/**
	 * @see au.com.handsunited.service.LanguageService#getAllLanguages()
	 */
	@Override
	public List<Language> getAllLanguages() {
		return languageDAO.getAllLanguages();
	}

	/**
	 * @see au.com.handsunited.service.LanguageService#getLanguage(java.lang.Long)
	 */
	@Override
	public Language getLanguage(Long id) {
		return languageDAO.read(id);
	}

	/**
	 * @see au.com.handsunited.service.LanguageService#getLanguage(java.lang.String)
	 */
	@Override
	public Language getLanguage(String id) {
		if (StringUtils.isNotBlank(id)) {
			return getLanguage(Long.parseLong(id));
		} else {
			return null;
		}
	}

	/**
	 * @see au.com.handsunited.service.LanguageService#updateLanguage(au.com.handsunited.model.language.Language)
	 */
	@Override
	public Language updateLanguage(Language language) {
		return languageDAO.update(language);
	}

	/**
	 * @see au.com.handsunited.service.LanguageService#getLanguagesMap()
	 */
	@Override
	public Map<Long, Language> getLanguagesMap() {

		Map<Long, Language> languagesMap = new TreeMap<Long, Language>();
		for (Language language : getAllLanguages()) {
			languagesMap.put(language.getId(), language);
		}
		return languagesMap;
	}



}
